package com.touch.jgloo.core;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import com.sun.opengl.util.texture.Texture;
import com.touch.core.util.XMLFile;
import com.touch.jgloo.JGLTextureLoader;
import com.touch.jgloo.owt.JGLAnimatedSprite;
import com.touch.jgloo.owt.JGLAnimation;

public class JGLAnimationRules 
{
	private String name;	
	private Vector<Vector<AnimationFrame>> frames = new Vector<Vector<AnimationFrame>>();	
	private String animationName[];
	private XMLFile rules;
	private String sprites;
	public JGLAnimationRules(InputStream xmlStream) throws SAXException, IOException, ParserConfigurationException
	{
		this.rules = new XMLFile(xmlStream);				
		this.name = rules.readValue("spriteformat", "name");	
		this.sprites = rules.readValue("spriteformat", "sprites");
		int animCount = Integer.parseInt(rules.readValue("spriteformat", "animations"));
				
		animationName = new String[animCount];
		
		for(int animIndex = 1; animIndex < animCount + 1; animIndex++)
		{
			Vector<AnimationFrame> currentFrame = new Vector<AnimationFrame>();
			int frameCount = Integer.parseInt(rules.readValue("spriteformat", "animation" + animIndex, "frames"));
			animationName[animIndex - 1] = rules.readValue("spriteformat", "animation" + animIndex, "name");
			
			for(int frameIndex = 1; frameIndex < frameCount + 1; frameIndex++)
			{
				AnimationFrame frame = new AnimationFrame();
				frame.sourceX = Integer.parseInt(rules.readValue("spriteformat", "animation" + animIndex, "frame" + frameIndex, "sourcex"));
				frame.sourceY = Integer.parseInt(rules.readValue("spriteformat", "animation" + animIndex, "frame" + frameIndex, "sourcey"));
				currentFrame.add(frame);
			}
			frames.add(currentFrame);
		}
	}		
	public String getName()
	{
		return this.name;
	}
	public String getSprites()
	{
		return this.sprites;
	}
	public void applyToAnimatedSprite(JGLAnimatedSprite sprite, BufferedImage sourceImg, int width, int height)	
	{		
		final Texture frame[][] = new Texture[sourceImg.getWidth() / width][sourceImg.getHeight() / height];
		//parse the image into textures		
		for(int x = 0; x < sourceImg.getWidth() / width; x++)
			for(int y = 0; y < sourceImg.getHeight() / height; y++)
			{
				final BufferedImage bFrame = sourceImg.getSubimage(x * width, y * height, width, height);
				frame[x][y] = JGLTextureLoader.loadIMG(bFrame, false);
			}
		
		for(int animIndex = 1; animIndex < this.frames.size() + 1; animIndex++)
		{
			final Vector<AnimationFrame> currentFrame = this.frames.elementAt(animIndex - 1);
			final JGLAnimation animation = new JGLAnimation(width, height);
			final String name = animationName[animIndex - 1];
			int frames = currentFrame.size();
			int speed = Integer.parseInt(rules.readValue("spriteformat", "animation" + animIndex, "speed"));
			for(int frameIndex = 1; frameIndex < frames + 1; frameIndex ++)
			{
				int x = currentFrame.get(frameIndex - 1).sourceX - 1;
				int y = currentFrame.get(frameIndex - 1).sourceY - 1;
				
				animation.addFrame(frame[x][y]);
			}
			animation.setFrameDuration(speed);
			sprite.addAnimation(name, animation);
		}							
	}
	public class AnimationFrame
	{
		
		public int sourceX;
		public int sourceY;
	}
	
}
